package com.cloud.school.bus.teacherhelper.modules.stuclass;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.utils.MobileUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.modules.settings.AboutActivity;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.RelationShipRequest;
import com.cloud.school.bus.teacherhelper.protocol.classmanage.RelationShipResponse;

/**
 * 添加学生，关联家长界面
 * 
 * @author jiahongfei
 * 
 */
public class AddStudentRelevanceParentActivity extends BaseActivity {

	private String mNameString;
	private String mParentPhoneString;
	private String mSexString;
	private String mBirthdayString;
	private String mPicBase64String;
	private String mFextString;
	private Student mStudent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.acitvity_add_student_relevance_parent);
		init();
	}

	@Override
	protected void init() {
		mNameString = getIntent().getStringExtra("name");
		mParentPhoneString = getIntent().getStringExtra("parentPhone");
		mSexString = getIntent().getStringExtra("sex");
		mBirthdayString = getIntent().getStringExtra("birthday");
		mPicBase64String = getIntent().getStringExtra("picbase64");
		mFextString = getIntent().getStringExtra("fext");
		mStudent = (Student) getIntent().getSerializableExtra("Student");

		TextView parentPhoneTextView = (TextView) findViewById(R.id.parentPhoneTextView);
		parentPhoneTextView.setText(mParentPhoneString);
		TextView studentNameTextView = (TextView) findViewById(R.id.studentNameTextView);
		studentNameTextView.setText(mStudent.student);

		setListener();
		setTitleNavBar();
	}
	
	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(getString(R.string.add_student));
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	@Override
	protected void setListener() {
		Button relevanceButton = (Button) findViewById(R.id.relevanceButton);
		relevanceButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				relationship();
			}
		});
		
		TextView callTextView = (TextView)findViewById(R.id.textView4);
		callTextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				MobileUtils.callPhoneActivity(mContext, AboutActivity.ABOUT_PHONE, Intent.ACTION_VIEW);
			}
		});
	}

	private void relationship() {
		String fsize = "";
		String fext = "";
		if (null != mPicBase64String && 0 != mPicBase64String.length()) {
			fsize = mPicBase64String.length() + "";
			fext = mFextString;
		}
		NetworkClient.getNetworkClient().PostRequest(
				new RelationShipRequest(mContext, mStudent.parentid,
						mNameString, mParentPhoneString, mSexString,
						mBirthdayString, mPicBase64String, fext, fsize),
				new RelationShipResponse() {
					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						if("1".equals(code)){
							mApplication.mStudentList.add(student);
							setResult(RESULT_OK);
							finish();
//							HandlerToastUI.getHandlerToastUI(mContext,
//									"添加成功");
						}else if ("-3".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"家长id 不能为空");
						}else if ("-4".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"不是老师操纵");
						}else if ("-2".equals(code)) {
							HandlerToastUI.getHandlerToastUI(mContext,
									"性别或生日姓名不能为空");
						}
					}
				});
	}
	
}
